package nbcp


import org.junit.Test
import nbcp.base.comm.JsonMap
import nbcp.base.extend.*

import nbcp.db.*
import nbcp.db.sql.*
import nbcp.db.mysql.*


class tool : TestBase() {

    @Test
    fun gen_db_entity() {
        var db = "nbcp"

        var tables_map = RawQuerySqlClip(SingleSqlData("""
SELECT table_name,table_comment
FROM INFORMATION_SCHEMA.TABLES
where table_schema = #db@
order by table_name
""", JsonMap("db" to db))).toMapList()

        var list = RawQuerySqlClip(SingleSqlData("""
SELECT table_name , column_name , data_type , column_comment
FROM INFORMATION_SCHEMA. COLUMNS
where table_schema = #db@
order by table_name , ordinal_position
""", JsonMap("db" to db))).toMapList()


        tables_map.forEach {
            var tableName = it.getStringValue("table_name");
            var tableComment = it.getStringValue("table_comment")

            var columns = list.filter { it.getStringValue("table_name") == tableName }
                    .map {
                        var columnName = it.getStringValue("column_name")
                        var dataType = it.getStringValue("data_type")
                        var columnComment = it.getStringValue("column_comment")

                        var kotlinType = "dataType"
                        var defaultValue = "(dataType)";

                        if (dataType VbSame "varchar") {
                            kotlinType = "String"
                            defaultValue = "\"\"";
                        } else if (dataType VbSame "int") {
                            kotlinType = "Int";
                            defaultValue = "0";
                        } else if (dataType VbSame "bit") {
                            kotlinType = "Boolean";
                            defaultValue = "false";
                        } else if (dataType VbSame "datetime") {
                            kotlinType = "LocalDateTime?"
                            defaultValue = "null"
                        } else if (dataType VbSame "date") {
                            kotlinType = "LocalDate?"
                            defaultValue = "null"
                        } else if (dataType VbSame "float") {
                            kotlinType = "Float"
                            defaultValue = "0F"
                        } else if (dataType VbSame "double") {
                            kotlinType = "Double"
                            defaultValue = "0"
                        } else if (dataType VbSame "long") {
                            kotlinType = "Long"
                            defaultValue = "0L"
                        }

                        if (columnComment.HasValue) {
                            """
//${columnComment}
var ${columnName}: ${kotlinType} = ${defaultValue}"""
                        } else {
                            """var ${columnName}: ${kotlinType} = ${defaultValue}"""
                        }
                    }

            println("""

//${tableComment}
data class ${tableName}(
    ${columns.joinToString(",\n").replace("\n", "\n\t")}
): IBaseDbEntity()

""")
        }
    }


//    @Test
//    fun gen_sql() {
//        var list = u_task::class.memberProperties.map {
//            var type = it.javaField!!.type.name
//
//            if (it.javaField!!.type == String::class.java) {
//                type = "varchar(50)"
//            }
//            if( it.javaField!!.type == Int::class.java || it.javaField!!.type.name == "java.lang.Integer"){
//                type = "int"
//            }
//
//            if( it.javaField!!.type == LocalDateTime::class.java){
//                type = "DateTime"
//            }
//
//            if( it.javaField!!.type == Boolean::class.java || it.javaField!!.type.name  == "java.lang.Boolean"){
//                type = "bit"
//            }
//
//            return@map "`${it.name}` ${type} not null comment ''"
//        }
//
//        println(list.joinToString(",\n"))
//    }
}